package com.ruike.alisurface.ui.mainPage;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.ruike.alisurface.R;
import com.ruike.alisurface.Serials.MsgEventBus;
import com.ruike.alisurface.base.BaseAty;
import com.ruike.alisurface.bean.ShopDetailBean;
import com.ruike.alisurface.http.OtherApiHttp;
import com.ruike.alisurface.ui.payPage.PayShowShopActivity;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Author: voodoo
 * CreateDate: 2024/03/14 014上午 10:56
 * Description: 取货码取货界面
 */
public class PickupCodeActivity extends BaseAty {

    @BindView(R.id.payPickupCode_countDown_tv)
    TextView countDownTv;
    @BindView(R.id.payPickupCode_pickupCode_et)
    EditText pickupCodeEt;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_pay_pickup_code;
    }

    @Override
    protected void initViews() {
        initTitleBar(false);
    }

    @Override
    protected void initData() {
    }

    @Override
    public void doEventBusResult(MsgEventBus event) {
    }

    @Override
    protected void onStart() {
        super.onStart();
        startCountdownTimer(120, countDownTv, null);
    }

    @OnClick({R.id.payPickupCode_goback_imgv, R.id.payPickupCode_submit_btn})
    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.payPickupCode_goback_imgv:
                startCountdownTimer(0, null, null);
                break;
            case R.id.payPickupCode_submit_btn:
                String inputCodeStr = pickupCodeEt.getText().toString().trim();
                if (TextUtils.isEmpty(inputCodeStr)) {
                    showErrorTip("请输入取货码");
                    return;
                }
                getProductSlotByInputCode(inputCodeStr);
                break;
        }
    }

    /**
     * 根据取货码去获取商品及货道等出货信息
     *
     * @param inputCodeStr 输入的取货码
     */
    private void getProductSlotByInputCode(String inputCodeStr) {
        showProgressDialog("正在验证取货码，请稍后...");
        OtherApiHttp.startOut(inputCodeStr, this);
    }

    @Override
    public void onSuccess(String requestUrl, String requestJsonStr) {
        super.onSuccess(requestUrl, requestJsonStr);
//        {"data":{"id":"2024031814572500136975","total_fee":0.01,"pay_fee":0.01,"details":[{"slotIndex":2,"quantity":1,"slotTypeId":21003,"price":0.01}]},"code":0,"msg":"Success","sendData":"2024-03-18 15:42:33"}

        String orderId = "";
        float totalFee = 0;
        float payFee = 0;
        List<ShopDetailBean.OrderDetailsBean> orderDetailsBeanList = new ArrayList<>();

        try {
            JSONObject jsonObject = new JSONObject(requestJsonStr);
            JSONObject jsonData = jsonObject.optJSONObject("data");
            if (jsonData == null) {
                return;
            }
            orderId = jsonData.optString("id");
            totalFee = (float) jsonData.optDouble("total_fee");
            payFee = (float) jsonData.optDouble("pay_fee");
            JSONArray arrayDetails = jsonData.optJSONArray("details");
            if (arrayDetails == null) {
                return;
            }
            for (int i = 0; i < arrayDetails.length(); i++) {
                JSONObject tempDetailObject = arrayDetails.getJSONObject(i);
                int slotIndex = tempDetailObject.getInt("slotIndex");
                int quantity = tempDetailObject.getInt("quantity");
                int slotTypeId = tempDetailObject.getInt("slotTypeId");
                ShopDetailBean.OrderDetailsBean orderDetailsBean = new ShopDetailBean.OrderDetailsBean();
                orderDetailsBean.setSlotIndex(slotIndex);
                orderDetailsBean.setQuantity(quantity);
                orderDetailsBean.setSlotTypeId(slotTypeId);
                orderDetailsBeanList.add(orderDetailsBean);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        ShopDetailBean shopDetailBean = new ShopDetailBean(orderId);
        shopDetailBean.setTotal_fee(totalFee);
        shopDetailBean.setPay_fee(payFee);
        shopDetailBean.setDetailOrderList(orderDetailsBeanList);
        Bundle bundle = new Bundle();
        bundle.putString("pay_type", "pickupCode");
        bundle.putSerializable("orderBeanData", shopDetailBean);
        startActivity(PayShowShopActivity.class, bundle);
        finish();
    }

    @Override
    public void onError(String requestUrl, String errorMsg) {
        super.onError(requestUrl, errorMsg);
        showErrorTip(errorMsg);
    }

}
